﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Resources;
using System.Threading;
using IGuess.KNN;

namespace IGuess
{
    public partial class ClassifierForm : Form
    {
        MainForm mainForm;
        string fileName;
        Classifier classifier;
        string textToClassify;

        public ClassifierForm(MainForm mainForm)
        {
            InitializeComponent();
            this.mainForm = mainForm;
            this.classifier = new Classifier();
        }

        private void ClassifierForm_Load(object sender, EventArgs e)
        {
            this.classifierFormSelectFileButton.Text = RM.GetString("classifierFormSelectFileButton");
            this.classifierFormSelectFileTextBox.Text = RM.GetString("classifierFormSelectFileTextBox");
            this.classifierFormProgressLabel.Text = RM.GetString("classifierFormProgressLabel");
            this.classifierFormClassifyButton.Text = RM.GetString("classifierFormClassifyButton");
            this.classifierFormAlgorithmLabel.Text = RM.GetString("classifierFormAlgorithmLabel");
            this.classifierFormAlgorithmComboBox.Text = RM.GetString("DefaultDropDownAction");
            this.classifierFormAlgorithmComboBox.Items.Add(RM.GetString("DefaultDropDownAction"));
            this.classifierFormAlgorithmComboBox.Items.Add(RM.GetString("NaiveBayesAlgorithm"));
            this.classifierFormAlgorithmComboBox.Items.Add(RM.GetString("ProbabilityAlgorithm"));
            this.classifierFormAlgorithmComboBox.Items.Add(RM.GetString("MAPAlgorithm"));
            this.classifierFormAlgorithmComboBox.Items.Add(RM.GetString("KNNAlgorithm"));
        }

        private void ClassifierForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
            this.mainForm.Visible = true;
            this.mainForm.Enabled = true;
        }

        private void classifierFormClassifyButton_Click(object sender, EventArgs e)
        {
            if (Path.GetExtension(this.fileName).Equals(".txt"))
            {
                try
                {
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(this.fileName))
                    {

                        this.textToClassify = sr.ReadToEnd();
                    }

                    try
                    {
                        if (this.classifierFormAlgorithmComboBox.SelectedItem.Equals(RM.GetString("NaiveBayesAlgorithm")))
                        {
                            BayesClassifier baclassifier = new BayesClassifier(this.textToClassify);
                            BayesForm bForm = new BayesForm(this, baclassifier, this.fileName);
                            bForm.Visible = true;
                            this.Enabled = false;

                        }
                        else if (this.classifierFormAlgorithmComboBox.SelectedItem.Equals(RM.GetString("ProbabilityAlgorithm")))
                        {

                            FrequenceClassifier fClassifier = new FrequenceClassifier(this.textToClassify);
                            FrequenceForm fForm = new FrequenceForm(this, fClassifier, this.fileName);
                            fForm.Visible = true;
                            this.Enabled = false;

                        }
                        else if (this.classifierFormAlgorithmComboBox.SelectedItem.Equals(RM.GetString("MAPAlgorithm")))
                        {

                            MAPClassifier MAPClassifier = new MAPClassifier(this.textToClassify);
                            MAPForm MAPForm = new MAPForm(this, MAPClassifier, this.fileName);
                            MAPForm.Visible = true;
                            this.Enabled = false;

                        }
                        else if (this.classifierFormAlgorithmComboBox.SelectedItem.Equals(RM.GetString("KNNAlgorithm")))
                        {

                            KNNClassifier knnClassifier = new KNNClassifier(this.textToClassify);
                            KNNForm knnForm = new KNNForm(this, knnClassifier, this.fileName);
                            knnForm.Visible = true;
                            this.Enabled = false;

                        }
                        else
                        {

                            MessageBox.Show(RM.GetString("AlgorithmNotFoundMessage"));

                        }
                    }
                    catch (NullReferenceException excpt)
                    {
                        MessageBox.Show(RM.GetString("AlgorithmNotFoundMessage") + "\n" + excpt.Message);
                    }

                }
                catch (ArgumentNullException ae)
                {

                    MessageBox.Show(RM.GetString("ExceptionInitialMessage") + ae.Message);

                }
                catch (FileNotFoundException fe)
                {

                    MessageBox.Show(RM.GetString("ExceptionInitialMessage") + fe.Message);

                }
                catch (InvalidOperationException ioe)
                {

                    MessageBox.Show(RM.GetString("ExceptionInitialMessage") + ioe.Message);

                }
            }
            else
                MessageBox.Show(RM.GetString("NotSupportedFileExtensionMessage"));
        }

        private void classifierFormSelectFileButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.classifierFormSelectFileTextBox.Text = openFileDialog1.FileName;
                this.fileName = openFileDialog1.FileName;
                try
                {
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(this.fileName))
                    {

                        this.classifierFormTextTextBox.Text = sr.ReadToEnd();
                        Thread.Sleep(1000);
                        progressBar1.Minimum = 0;
                        progressBar1.Maximum = 100;
                        progressBar1.Value = 100;

                    }
                }
                catch (FileNotFoundException fe)
                {

                    MessageBox.Show(RM.GetString("ExceptionInitialMessage") + fe.Message);

                }
                
            }
        }

        private void classifierFormSelectFileTextBox_MouseClick(object sender, MouseEventArgs e)
        {
            this.classifierFormSelectFileTextBox.Text = "";
        }

        private void classifierFormSelectFileTextBox_TextChanged(object sender, EventArgs e)
        {
            this.fileName = this.classifierFormSelectFileTextBox.Text;
        }

        private void classifierFormSelectFileTextBox_Leave(object sender, EventArgs e)
        {

            this.classifierFormSelectFileTextBox.Text = RM.GetString("classifierFormSelectFileTextBox");

        }
    }
}
